package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.NewsVo;
import com.smart.village.big.screen.mapper.BenefitPeopleMapper;
import com.smart.village.big.screen.mapper.MicroSmartMoHeResidentMapper;
import com.smart.village.big.screen.mapper.NewsMapper;
import com.smart.village.big.screen.service.BenefitPeopleService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BenefitPeopleServiceImpl implements BenefitPeopleService {
    @Autowired
    private BenefitPeopleMapper benefitPeopleMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private MicroSmartMoHeResidentMapper residentMapper;
    @Override
    public JSONObject getPersonalPointsAccountList(String townNumber, String villageNumber, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> personalPointsAccountList = benefitPeopleMapper.getPersonalPointsAccountList(townNumber, villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getProjectProcessList(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> personalPointsAccountList = benefitPeopleMapper.getProjectProcessList(townNumber, villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getThreeAffairsDisclosureList(String townNumber, String villageNumber, String type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> personalPointsAccountList = benefitPeopleMapper.getThreeAffairsDisclosureList(townNumber, villageNumber,type);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getThreeResourceDisclosureList(String townNumber, String villageNumber, String category, int pageNum, int pageSize,String NewsTerminal) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> personalPointsAccountList = benefitPeopleMapper.getThreeResourceDisclosureList(townNumber, villageNumber,category,NewsTerminal);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getThreeAffairsDisclosureDetail(String affairsSerialNumber) {
        JSONObject jsonObject =  benefitPeopleMapper.getThreeAffairsDisclosureDetail(affairsSerialNumber);
        jsonObject.put("Attachment_List",benefitPeopleMapper.getThreeAffairsDisclosureDetailAttachmentList(affairsSerialNumber));
        return jsonObject;
    }

    @Override
    public JSONObject getPolicyAdvocacy(int pageNum, int pageSize,String NewsTerminal) {
        PageHelper.startPage(pageNum, pageSize);
        List<String> treeList = Lists.newArrayList();
        treeList.add("NC0000000000071");
        List<NewsVo> newsVos = newsMapper.selectNewsVoListInCategorySerialNumberList(treeList, "","",0,NewsTerminal);
        PageInfo<NewsVo> pageInfo = new PageInfo<>(newsVos);
        execFormatSubTitle(newsVos);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("news", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getVillageRegulationsAgreements(String townNumber, String villageNumber) {
        return benefitPeopleMapper.getVillageRegulationsAgreements(townNumber,villageNumber);
    }

    @Override
    public List<JSONObject> getVillageConcatPersonList(String townNumber, String villageNumber) {
        if(villageNumber == null || villageNumber.isEmpty()){
            return Lists.newArrayList();
        }
        return benefitPeopleMapper.getVillageConcatPersonList(villageNumber);
    }

    @Override
    public JSONObject getListManagement(String listTypeSerialNumber, String townNumber, String villageNumber, String level, String search, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> listManagement = residentMapper.getListManagement(listTypeSerialNumber,townNumber,villageNumber,level,search);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(listManagement);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getListManagementDetail(String listManagementSerialNumber) {
        JSONObject jsonObject =  residentMapper.getListManagementDetail(listManagementSerialNumber);
        jsonObject.put("List_Material_List", residentMapper.getListMaterial(listManagementSerialNumber));
        jsonObject.put("List_Management_Attachment", residentMapper.getListManagementAttachment(listManagementSerialNumber));
        return jsonObject;
    }

    @Override
    public JSONObject getVolunteerActivityList(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> listManagement = benefitPeopleMapper.getVolunteerActivityList(townNumber,villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(listManagement);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }


    private void execFormatSubTitle(List<NewsVo> newsVoList) {
        newsVoList.forEach(newsVo -> {
            String newsSubTitle = newsVo.getNewsSubTitle();
            Document doc = Jsoup.parse(newsSubTitle);

            // 查找第一个<p>标签
            Elements paragraphs = doc.select("p");
            if (paragraphs.size() > 0) {
                Element firstParagraph = paragraphs.first(); // 获取第一个<p>标签
                String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                if (content.length() > 70) {
                    content = content.substring(0, 67) + "...";
                }
                newsVo.setNewsSubTitle(content);
                return;
            } else {
                Elements h1 = doc.select("h1");
                Elements h2 = doc.select("h2");
                Elements h3 = doc.select("h3");
                Elements h4 = doc.select("h4");
                Elements h5 = doc.select("h5");
                Elements ul = doc.select("ul");
                Elements ol = doc.select("ol");
                Elements li = doc.select("li");
                Elements div = doc.select("div");
                Elements span = doc.select("span");
                Elements strong = doc.select("strong");
                if (h1.size() > 0) {
                    Element firstParagraph = h1.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                }
                if (h2.size() > 0) {
                    Element firstParagraph = h2.first(); // 获取第一个<h2>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                }
                if (h3.size() > 0) {
                    Element firstParagraph = h3.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                }
                if (h4.size() > 0) {
                    Element firstParagraph = h4.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                }
                if (h5.size() > 0) {
                    Element firstParagraph = h5.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (ul.size() > 0) {
                    Element firstParagraph = ul.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (ol.size() > 0) {
                    Element firstParagraph = ol.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (div.size() > 0) {
                    Element firstParagraph = div.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (span.size() > 0) {
                    Element firstParagraph = span.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (strong.size() > 0) {
                    Element firstParagraph = strong.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                } if (li.size() > 0) {
                    Element firstParagraph = li.first(); // 获取第一个<h1>标签
                    String content = firstParagraph.text(); // 获取标签的内容 如果超长 则处理为...
                    if (content.length() > 70) {
                        content = content.substring(0, 67) + "...";
                    }
                    newsVo.setNewsSubTitle(content);
                    return;
                }
            }
        });
    }
}
